package com.forotuxpan.appforotuxpan;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link DirectorioFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link DirectorioFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DirectorioFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    List<Row> rows;
    private ListView lstListado;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Directorio.
     */
    // TODO: Rename and change types and number of parameters
    public static DirectorioFragment newInstance(String param1, String param2) {
        DirectorioFragment fragment = new DirectorioFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public DirectorioFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_directorio, container, false);
    }

    @Override
    public void onActivityCreated(Bundle state) {
        super.onActivityCreated(state);
        lstListado = (ListView)getView().findViewById(R.id.LstListado);

        fillABC();

        lstListado.setAdapter(new ItemAdapterABC(lstListado.getContext(), rows));
        lstListado.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //pasarActividad2(view);
                //CheckBox checkbox = (CheckBox) v.getTag(R.id.chkAvisado);
                //TextView txtTitle = (TextView) v.getTag(R.id.textViewTitle);
                //TextView txtSubTitle = (TextView) v.getTag(R.id.textViewSubtitle);
                //Toast.makeText(getActivity(), "Pendiente ir a...", Toast.LENGTH_LONG).show();
                //Toast.makeText(lstListado.getContext(), rows.get(position).getTitle(), Toast.LENGTH_SHORT).show();

                Intent i = new Intent(view.getContext(), EmpresasActivity.class);
                i.putExtra("Letra", rows.get(position).getTitle());
                startActivity(i);
            }
        });
    }

    private void fillABC()
    {
        rows = new ArrayList<Row>(24);
        Row row = null;
        row = new Row();
        row.setTitle("A");
        rows.add(row);

        row = new Row();
        row.setTitle("B");
        rows.add(row);

        row = new Row();
        row.setTitle("C");
        rows.add(row);

        row = new Row();
        row.setTitle("D");
        rows.add(row);

        row = new Row();
        row.setTitle("E");
        rows.add(row);

        row = new Row();
        row.setTitle("F");
        rows.add(row);

        row = new Row();
        row.setTitle("G");
        rows.add(row);

        row = new Row();
        row.setTitle("H");
        rows.add(row);

        row = new Row();
        row.setTitle("I");
        rows.add(row);

        row = new Row();
        row.setTitle("J");
        rows.add(row);

        row = new Row();
        row.setTitle("K");
        rows.add(row);

        row = new Row();
        row.setTitle("L");
        rows.add(row);

        row = new Row();
        row.setTitle("M");
        rows.add(row);

        row = new Row();
        row.setTitle("N");
        rows.add(row);

        row = new Row();
        row.setTitle("O");
        rows.add(row);

        row = new Row();
        row.setTitle("P");
        rows.add(row);

        row = new Row();
        row.setTitle("Q");
        rows.add(row);

        row = new Row();
        row.setTitle("R");
        rows.add(row);

        row = new Row();
        row.setTitle("S");
        rows.add(row);

        row = new Row();
        row.setTitle("T");
        rows.add(row);

        row = new Row();
        row.setTitle("U");
        rows.add(row);

        row = new Row();
        row.setTitle("V");
        rows.add(row);

        row = new Row();
        row.setTitle("W");
        rows.add(row);

        row = new Row();
        row.setTitle("X");
        rows.add(row);

        row = new Row();
        row.setTitle("Y");
        rows.add(row);

        row = new Row();
        row.setTitle("Z");
        rows.add(row);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

}
